package com.xx.douyi.service.impl;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.xx.douyi.service.DouYinService;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.net.HttpCookie;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Pattern;

@Service
public class DouYinServiceImpl implements DouYinService {
    @Override
    public List<HttpCookie> GoDy() {
        List<HttpCookie> cookie = HttpUtil.createGet("https://live.douyin.com/").execute().getCookie();
        return cookie;
    }

    @Override
    public HashMap getLive(HttpServletRequest request) {
        HashMap<String, String> headers = new HashMap<>();
        StringBuffer s = getCookie(request);
        headers.put("cookie",s.toString());
        String body = HttpUtil.createGet("https://live.douyin.com/webcast/room/web/enter/?aid=6383&web_rid="+ ReUtil.getGroup0("\\d+",request.getParameter("url"))).addHeaders(headers).execute().body();
        HashMap hashMap = JSON.parseObject(body, HashMap.class);
        return hashMap;
    }

    @NotNull
    private StringBuffer getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        StringBuffer s = new StringBuffer();
        for (Cookie cookie : cookies) {
            s.append(cookie.getName()+"="+cookie.getValue()+";");
        }
        return s;
    }

    @Override
    public Map getUsers(HttpServletRequest request) {
        HashMap<String, String> headers = new HashMap<>();
        StringBuffer s = getCookie(request);
        headers.put("cookie",s.toString());
        String body = HttpUtil.createGet("https://live.douyin.com/webcast/web/enter/?aid=6383&web_rid="+ ReUtil.getGroup0("\\d+",request.getParameter("url"))).addHeaders(headers).execute().body();
        HashMap hashMap = JSON.parseObject(body, HashMap.class);
        return hashMap;
    }

    @Override
    public Map getQrCode() {
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Referer","https://sso.douyin.com/");
        headers.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
        headers.put("Cookie","ttwid=1%7C7tTWRLJ-d-vgXTHRihUOR7Z7FsjsAna6whjK1VIwFu0%7C1671095968%7Cbe7b03064775254f8bb78ab45f931ff0c3a11095d77323593ea39d2f402e3325;");
        String body = HttpUtil.createGet("https://sso.douyin.com/get_qrcode/?next=https:%2F%2Fcreator.douyin.com%2Fcreator-micro%2Fhome&aid=2906&service=https:%2F%2Fcreator.douyin.com&is_vcd=1&f=").addHeaders(headers).execute().body();
        HashMap hashMap = JSON.parseObject(body, HashMap.class);
        return hashMap;
    }

    @Override
    public Map checkQrConnect(String token) {
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Referer","https://sso.douyin.com/");
        headers.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
        headers.put("Cookie","ttwid=1%7C7tTWRLJ-d-vgXTHRihUOR7Z7FsjsAna6whjK1VIwFu0%7C1671095968%7Cbe7b03064775254f8bb78ab45f931ff0c3a11095d77323593ea39d2f402e3325;");
        HttpResponse response = HttpUtil.createGet("https://sso.douyin.com/check_qrconnect/?next=https:%2F%2Fcreator.douyin.com%2Fcreator-micro%2Fhome&token=" + token + "&service=https:%2F%2Fcreator.douyin.com%2F%3Flogintype%3Duser%26loginapp%3Ddouyin%26jump%3Dhttps:%2F%2Fcreator.douyin.com%2Fcreator-micro%2Fhome&correct_service=https:%2F%2Fcreator.douyin.com%2F%3Flogintype%3Duser%26loginapp%3Ddouyin%26jump%3Dhttps:%2F%2Fcreator.douyin.com%2Fcreator-micro%2Fhome&aid=2906&is_vcd=1").addHeaders(headers).execute();
        String cookie = getCookie(response);
        HashMap hashMap = JSON.parseObject(response.body(), HashMap.class);
        HttpResponse login = isLogin(response);
        HashMap loginMap = JSON.parseObject(login.body(), HashMap.class);
        if (loginMap.get("has_login").equals(true)) {
             cookie += getCookie(login);
        }
        cookie = cookie.replaceAll("\n","");
        hashMap.put("cookie",cookie);
        return hashMap;


    }

    @Override
    public Map getStream(String cookie, String did) {
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Cookie",cookie);
        HttpResponse res = HttpUtil.createGet("https://webcast.amemv.com/webcast/room/continue/?device_id="+did+"&aid=1128").addHeaders(headers).execute();
        Map body = JSON.parseObject(res.body(),HashMap.class);
        return body;
    }

    @Override
    public Map getUserAllVideo(String url, String maxCursor) {
        String cookie = "s_v_web_id=verify_lexnuz9i_jWQ6egcD_wOZE_4TGN_97ut_BUXgyIlhtK8b;";
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Cookie",cookie);
        HttpResponse res = HttpUtil.createGet("https://m.douyin.com/web/api/v2/aweme/post/?reflow_source=reflow_page&sec_uid="+ReUtil.getGroup0("[^\\/]+(?!.*\\/)",url)+"&count=21&max_cursor="+maxCursor).addHeaders(headers).execute();
        Map body = JSON.parseObject(res.body(),HashMap.class);
        return body;
    }

    public HttpResponse isLogin(HttpResponse response){
        HashMap<String, String> headers = new HashMap<>();
        String cookie = getCookie(response);
        headers.put("Cookie",cookie);
        HttpResponse res = HttpUtil.createGet("https://sso.douyin.com/check_login/?service=https:%2F%2Fwww.douyin.com&aid=6383").addHeaders(headers).execute();
        return res;
    }
    public HttpResponse getCookieByHome(HttpResponse response){
        HashMap<String, String> headers = new HashMap<>();
        String cookie = getCookie(response);
        headers.put("Cookie",cookie);
        HttpResponse res = HttpUtil.createGet("http://douyin.com").addHeaders(headers).execute();
        return res;
    }
    public String getCookie(HttpResponse response){
        List<String> setCookie = response.headerList("Set-Cookie");
        String join = "";
        for (String s : setCookie) {
            String group = ReUtil.getGroup0("^.+?;", s);
            join+=group;
        }
        return join;
    }
}

class  Uitl{
    public static void main(String[] args) {
        System.out.println(isAdmin("Admin"));
    }
    public  static  boolean isAdmin(String user){
        System.out.println(user.toLowerCase());
        String s = user.toLowerCase();
        return s == "admin";
    }
}
